package com.voxel.sense.infomsg.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.amap.api.maps.AMap;
import com.example.com.common.app.BaseContract;
import com.example.com.common.factory.data.DataSource;
import com.example.com.common.utils.L;
import com.voxel.sense.infomsg.module.home.HomeContract;
import com.voxel.sense.infomsg.module.home.HomePresenter;

import java.util.ArrayList;
import java.util.List;

import cyy.example.com.facotry.net.helper.NetHelper;

import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.share.SPEngine;

/**
 * Created by ChenYiYao on 2017/11/19.
 */

public class UpLocationService extends Service {
    private long mUpInterval = 1000 * 2; //刷新时间

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mUpInterval = SPEngine.getSPEngine().getUserInfo().getUplocationTime();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        creaAlarm();
        upLocation();
        return super.onStartCommand(intent, flags, startId);
    }

    private void upLocation() {
        try {
            String token = SPEngine.getSPEngine().getUserInfo().getSetMobileToken();
            L.d("upLocation-----token-----"+token);
            if (TextUtils.isEmpty(token))
                return;

            double latitude = Double.parseDouble(SPEngine.getSPEngine().getUserInfo().getlatitude());
            double longgiude = Double.parseDouble(SPEngine.getSPEngine().getUserInfo().getLonggiude());
            L.d("-----上传位置-----");
            clickCard(latitude, longgiude);
            NetHelper.UpMyLocation(latitude, longgiude, new DataSource.Callback<BaseEntity>() {
                @Override
                public void onDataNotAvailable(String strRes, int reqId) {
                }

                @Override
                public void onDataLoaded(BaseEntity data, int reqId) {
                }
            });
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }

    private void clickCard(double latitude, double longgiude) {
        NetHelper.UserClockIn(latitude, longgiude, new DataSource.Callback<BaseEntity>() {
            @Override
            public void onDataNotAvailable(String strRes, int reqId) {
            }

            @Override
            public void onDataLoaded(BaseEntity data, int reqId) {

            }
        });
    }

    private void creaAlarm() {
        AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getApplicationContext().ALARM_SERVICE);
        Intent intent = new Intent(getApplication(), UpLocationService.class);
        PendingIntent sender = PendingIntent.getService(getApplication(), -1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        long time = SystemClock.elapsedRealtime() + mUpInterval;
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, sender);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //销毁，重新启动
    }
}
